<?php
namespace app\api\common;

use app\common\model\Config as ConfigModel;
use app\util\CurlRequest;
use think\Cache;
use fast\Random;

class Program
{
    /**
     * 获取小程序AccessToken
     */
    public static function getAccessToken()
    {
        //获取access_token
        $appid = ConfigModel::getConfigValue("wx_appid");
        $secret = ConfigModel::getConfigValue("wx_appsecret");
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        $lifeTime = 2 * 3000;
        //Cache::rm('access_token');   //清空缓存使用
        $access_token = Cache::get('access_token');
        if(empty($access_token)){
            $access_token_data = file_get_contents($url);
            $access_token_data = json_decode($access_token_data,true);
            $access_token = $access_token_data['access_token'];
            Cache::set('access_token',$access_token,$lifeTime);
        }
        return ['code'=>200,'msg'=>'获取成功','data'=>["access_token"=>$access_token]];
    }

    /**
     * 文本内容安全识别
     */
    public static function TextRecognition($content,$openid)
    {
        $access_token_data = self::getAccessToken();
        if ($access_token_data["code"]!=200){
            return $access_token_data;
        }
        $access_token = $access_token_data["data"]["access_token"];
        $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$access_token;
        $data = [
            "content" => $content,
            "version" => 2,
            "scene" => 1,
            "openid" => $openid,
        ];
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $result = CurlRequest::sendCurlPost($url,$data);
        $result = json_decode($result,true);
        if($result['errcode'] == 0){
            $response = $result['result'];
            if($response['label'] != 100){
                $response_msg = "";
                switch ($response["label"]) {
                    case 10001:
                        $response_msg = "广告";
                        break;
                    case 20001:
                        $response_msg = "时政";
                        break;
                    case 20002:
                        $response_msg = "色情";
                        break;
                    case 20003:
                        $response_msg = "辱骂";
                        break;
                    case 20006:
                        $response_msg = "违法犯罪";
                        break;
                    case 20008:
                        $response_msg = "欺诈";
                        break;
                    case 20012:
                        $response_msg = "低俗";
                        break;
                    case 20013:
                        $response_msg = "版权";
                        break;
                    case 21000 :
                        $response_msg = "其它";
                        break;
                }
                return ['code'=>200,'label'=>$response['label'],'data'=>$response_msg];
            }else{
                return ['code'=>200,'label'=>$response['label'],'data'=>''];
            }
        }else{
            return ['code'=>201,'label'=>'请求失败','data'=>$result];
        }
    }

    /**
     * 音视频内容安全识别
     */
    public static function VideoRecognition($media_url,$media_type,$openid)
    {
        $access_token_data = self::getAccessToken();
        if ($access_token_data["code"]!=200){
            return $access_token_data;
        }
        $access_token = $access_token_data["data"]["access_token"];
        $url = "https://api.weixin.qq.com/wxa/media_check_async?access_token=".$access_token;
        $data = [
            "media_url" => $media_url,
            "media_type" => $media_type, //1:音频;2:图片
            "version" => 2,
            "scene" => 1,
            "openid" => $openid,
        ];
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $result = CurlRequest::sendCurlPost($url,$data);
        $result = json_decode($result,true);
        if($result['errcode'] == 0){
            if(isset($result['result'])){
                $response = $result['result'];
                if($response['label'] != 100){
                    $response_msg = "";
                    switch ($response["label"]) {
                        case 20001:
                            $response_msg = "时政";
                            break;
                        case 20002:
                            $response_msg = "色情";
                            break;
                        case 20006:
                            $response_msg = "违法犯罪";
                            break;
                        case 21000 :
                            $response_msg = "其它";
                            break;
                    }
                    return ['code'=>200,'label'=>$response['label'],'data'=>$response_msg];
                }else{
                    return ['code'=>200,'label'=>$response['label'],'data'=>''];
                }
            }else{
                return ['code'=>200,'label'=>100,'data'=>''];
            }
        }else{
            return ['code'=>201,'label'=>'请求失败','data'=>$result];
        }
    }

    public static function getSignature()
    {
        $access_token_data = self::getAccessToken();
        if ($access_token_data["code"]!=200){
            return $access_token_data;
        }
        $token = $access_token_data["data"]["access_token"];
        $timestamp = time();
        $nonce = Random::nozero(8);
        // 将参数放入数组
        $params = [$token, $timestamp, $nonce];
        // 对数组进行字典序排序
        sort($params, SORT_STRING);

        // 将排序后的参数拼接成一个字符串
        $sortedParamsString = implode("", $params);

        // 计算 SHA1 签名
        $signature = sha1($sortedParamsString);
        return $signature;
    }

    public static function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $access_token_data = self::getAccessToken();
        if ($access_token_data["code"]!=200){
            return $access_token_data;
        }
        $token = $access_token_data["data"]["access_token"];
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        return $tmpStr === $signature;
    }


}